<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(__FILE__)) . '/appwap.php');
$searchName = isset($_GET['searchName'])?trim(strip_tags($_GET['searchName'])):'';
$start_time = isset($_GET['start_time'])?$_GET['start_time']:'';
$search_end_time = isset($_GET['end_time'])?$_GET['end_time']:'';
$daytime = strtotime(date('Y-m-d'));
$condition = array(
	'team_type' => 'normal',
	"begin_time <= '{$daytime}'",
);
if(!empty($start_time)){
$s_time = strtotime($start_time);
$condition []= "( begin_time >= '{$s_time}' )";
}
if(!empty($search_end_time)){
$e_time = strtotime($search_end_time);
$condition []= "( begin_time <= '{$e_time}' )";
}
if($searchName == '精准查找团购，这里搜索！' || $searchName == ""){
  $searchName = '团购';
}else{
  $condition[] = "( title like '%".mysql_escape_string($searchName)."%' )";
}
$city_id = abs(intval($city['id']));
$condition[] = "((city_ids like '%@{$city_id}@%' or city_ids like '%@0@%') or city_id in(0,{$city_id}))";

if (!option_yes('displayfailure')) {
	$condition['OR'] = array(
		"now_number >= min_number",
		"end_time > '{$daytime}'",
	);
}

$group_id = abs(intval($_GET['gid']));
if ($group_id) $condition['group_id'] = $group_id;

$count = Table::Count('team', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 12);
$teams = DB::LimitQuery('team', array(
	'condition' => $condition,
	'order' => 'ORDER BY begin_time DESC, sort_order DESC, id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));


$now = time();
$detail = array();

foreach($teams AS $index => $team) {

	if($team['end_time']<$team['begin_time']){$team['end_time']=$team['begin_time'];}
	$diff_time = $left_time = $team['end_time']-$now;
	if ( $team['team_type'] == 'seconds' && $team['begin_time'] >= $now ) {
		$diff_time = $left_time = $team['begin_time']-$now;
	}

	/* progress bar size */
	$detail[$team['id']]['bar_size'] = ceil(190*($team['now_number']/$team['min_number']));
	$detail[$team['id']]['bar_offset'] = ceil(5*($team['now_number']/$team['min_number']));

	$left_day = floor($diff_time/86400);
	$left_time = $left_time % 86400;
	$left_hour = floor($left_time/3600);
	$left_time = $left_time % 3600;
	$left_minute = floor($left_time/60);
	$left_time = $left_time % 60;
    
	$detail[$team['id']]['diff_time'] = $diff_time;
	$detail[$team['id']]['left_day'] = $left_day;
	$detail[$team['id']]['left_hour'] = $left_hour;
	$detail[$team['id']]['left_minute'] = $left_minute;
	$detail[$team['id']]['left_time'] = $left_time;
	$detail[$team['id']]['is_today'] = $team['begin_time'] + 3600*24 > time() ? 1:0;
    $partner = Table::Fetch('partner', $team['partner_id']);
    $team['address']=$partner['address'];
 
	$categorys = DB::LimitQuery('category', array(
		'condition' => array( 'zone' => 'area','id' =>$team['area'],'display' => 'Y' ),
		'order' => 'ORDER BY sort_order DESC, id DESC',
	));	
	
	
	
	$team['a'] =  $categorys[0]['name'];	
    //$team['open'] = $partner['open'];
    //$team['title'] = $partner['title'];
    //echo  $team['address'];
	/* state */
	$team['state'] = team_state($team);
	$teams[$index] = $team;
	
	
}
foreach($teams AS $id=>$one){
	team_state($one);
	if (!$one['close_time']) $one['picclass'] = 'isopen';
	if ($one['state']=='soldout') $one['picclass'] = 'soldout';
	$teams[$id] = $one;
}
$searchName_show = '“'.$searchName.'”';
$category = Table::Fetch('category', $group_id);
$pagetitle = '收索结果';
$current_team = 'class="current"';
//include template('team_index');
include template('wap_touch_search');

function current_teamcategory($gid='0') {
	global $city;
	$a = array(
			'/team/index.php' => '所有',
			);
    $categorys = DB::LimitQuery('category', array(
		'condition' => array( 'zone' => 'group','fid' => '0','display' => 'Y' ),
		'order' => 'ORDER BY sort_order DESC, id DESC',
	));
	$categorys = Utility::OptionArray($categorys, 'id', 'name');
	foreach($categorys AS $id=>$name) {
		$a["/team/index.php?gid={$id}"] = $name;
	}
	$l = "/team/index.php?gid={$gid}";
	if (!$gid) $l = "/team/index.php";
	return current_link($l, $a, true);
}
